$(document).ready(function() {
	if (det.sort ==1 && det.uid > 0  ) {
		if (det.privilege == 0) {
			$('.footers').removeClass('shangpinduihuan')
			$('.footers').addClass('querenduihuans')
			$('.detail_prodect_guige').removeClass('detail_prodect_guiges')
			$('.querenduihuans').html('活动专享')
		}

		else if(det.limit_time == 1){
			$('.footers').removeClass('shangpinduihuan')
			$('.footers').addClass('querenduihuans')
			$('.detail_prodect_guige').removeClass('detail_prodect_guiges')
			$('.querenduihuans').html('限购一次')
		}else{
			$('.footers').addClass('shangpinduihuan')
			$('.footers').removeClass('querenduihuans')
			$('.detail_prodect_guige').addClass('detail_prodect_guiges')
		}
	
}else{
	$('.footers').addClass('shangpinduihuan')
	$('.footers').removeClass('querenduihuans')
	$('.detail_prodect_guige').addClass('detail_prodect_guiges')

}



	// 产品轮播 
	var swiper = new Swiper('.swiper-detail', {
		pagination: '.swiper-pagination',
		paginationClickable: true
	});
	// 初始化规格参数
	$('.detail_select_xh li:first').addClass('cur');
	$('.detail_select_xinghao').html($('.detail_select_xh li:first').html())
		// 选择加减
	var number = $('.numberinput')
	
jiajians()
	function jiajians(){

		for (var i = 0; i < number.length; i++) {
			number[i].onclick = function(e) {
				var e = e || window.event;
				var el = e.target || e.srcElement;
				var cls = el.className;
				var countInout = this.getElementsByTagName('input')[0];
				var value = parseInt(countInout.value);
				switch (cls) {
					case 'add':
						if (det.sort ==1 && det.privilege==1 && det.limit_num !=0 && det.uid > 0) {
							//限购数量
							if(countInout.value <det.limit_num){
								countInout.value = value + 1;
							}else{
								alert('此商品最多购买'+det.limit_num + '个')
							}
							$(this).find('.reduce').removeClass('pb')
							break;

					 	}else{// if(det.limit_num == 0)
					 		//没有限制数量
							if (value < parseInt($('#kucun').text())   ) {
								countInout.value = value + 1;
								$(this).find('.reduce').removeClass('pb')
							 	break;
							 }else {
								countInout.value = value;
								$(this).find('.reduce').removeClass('pb')
								break;
							}
						}
					case 'reduce':
						if (value > 1) {
							countInout.value = value - 1;
							if (countInout.value == 1) {
								$(this).find('.reduce').addClass('pb')
							}
						}
						break;
					case 'delete':
						if (conf) {
							this.parentNode.removeChild(this);
						}
						break;
				}
			}
		}
	}
	// 关闭
	$('.detail_select_close').click(function(event) {
		$('.detail_select').hide();
		$('.detail_mask').hide();
	});
	$('.detail_prodect_guiges ,.shangpinduihuan').click(function(event) {
		$('.detail_select').show();
		$('.detail_mask').show();
	});
	var attr_id = $('.detail_select_xh li:first').attr('data-c')
	function kuc() {
		$.ajax({
			url: kucun,
			type: 'POST',
			data: {
				attr_id: attr_id
			},
			success: function(data) {


				$('#kucun').html(JSON.parse(data).data.inventory)
			}
		})
	}
	kuc()
	$('.detail_select_yanse li ,.detail_select_xh li').click(function(event) {
		
			$('.numberinput input').val(1)

		jiajians()
		$(this).addClass('cur').siblings().removeClass('cur');
		$('.detail_select_xinghao').html($(this).html())
		attr_id = $(this).attr('data-c')
		kuc()
	});

	$('.querenduihuan').click(function(event) {

		
		// 查询数据库库存
		var attr_id = '';
		$('.detail_select_xh li').each(function(index, el) {
			if ($(this).hasClass('cur')) {
				attr_id = $(this).attr('data-c');
				return attr_id;
			}
		});
		var product_id = decodeURIComponent((new RegExp('[?|&]product_id=' + '([^&;]+?)(&|#|;|$)', "ig").exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
		var num = $('#num').val();
		$.ajax({
			url: confirm_order,
			type: 'post',
			data: {
				attr_id: attr_id,
				product_id: product_id,
				num: num
			},
			success: function(data) {
				// 存入属性cookie
				var arr = {};
				arr.product_id = product_id
				arr.attr_id = attr_id
				arr.num = num
				var product_order = JSON.stringify(arr);
				SetCookie('product_order', product_order)
				var data = JSON.parse(data);
				if (data.code == 1) {
					 window.location.href = localhosthref+ 'confirm_order_detail?product_id='+product_id
				} else {
					alert(data.desc)
				}
			},
			error:function(xhr, textStatus){
				if(xhr.status == 401){
					 window.location.href = "/Npoint_store/Login";
				}else{
					alert('系统错误')
				}

			}
		})
	});

});